import requests
import time
import sys
sys.path.append("../")
from model.StockBaseInfo import *
from frameworks.utils.PrettyTableUtil import *
"""
#f2:最新价 f3:涨幅 f4:涨跌额 f5:成交量 f6成交额 f7:振幅
#f8:换手率 f9:市盈率 f10:量比 f11: f12:代码 f14:股票名称 f15:最高
#f16:最低 f17:今开 f18:昨收 f20:总市值 f21:流通市值
#f22:
"""

base = StockBaseInfo()
def getMiniteData(code,pre):
    dfcfCode = base.translateDfcfCode(code)
    response = requests.get("https://push2.eastmoney.com/api/qt/stock/trends2/get?secid="+dfcfCode+"&fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=e1e6871893c6386c5ff6967026016627&iscr=0&isqhquote=")
    data = response.json()
    i = 1
    score = 0
    for row in data["data"]["trends"]:
        line = row.split(",")
        close = float(line[2])
        money = line[6]
        avag = line[7]
        zf = (close-pre)/pre
        score += zf*i;
        i += 1
    return round(score,2)

codes = []
end = False
for i in range(1,10):
    time.sleep(1)
    response = requests.get("https://97.push2.eastmoney.com/api/qt/clist/get?pn="+str(i)+"&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1735621995207")
    data = response.json()
    for row in data["data"]["diff"]:
        marketId = row["f12"][:1]
        if (marketId == "8" or marketId == "3"):
            if row["f3"] > 12 and row["f2"] == row["f15"]:
                codes.append({"codename":row["f14"],"code":row["f12"],"zf":row["f3"],"pre":float(row["f18"])})
            else:
                continue
        elif (marketId == "6" or marketId == "0") and row["f3"] > 9.3 and row["f2"] == row["f15"]:
            codes.append({"codename":row["f14"],"code":row["f12"],"zf":row["f3"],"pre":float(row["f18"])})
        else:
            print(row)
            end = True
            break
    if end:
        break

prettyTable = PrettyTableUtil(["证券代码","证券名称","score"])
for line in codes:
    prettyTable.addRow([line["code"],line["codename"],getMiniteData(line["code"],line["pre"])])

print(prettyTable.renderTable("score"))